package com.example.final_project.Home.adapter;

import android.content.Context;
import android.content.Intent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.bumptech.glide.Glide;
import com.example.final_project.Home.Activity.TimeInfoActivity;
import com.example.final_project.Home.entry.TimeInfo;
import com.example.final_project.R;

import java.util.ArrayList;
import java.util.List;

public class TimeInfoAdapter extends RecyclerView.Adapter {

    private Context context;
    private List<TimeInfo> infos;

    public TimeInfoAdapter(Context context,List<TimeInfo> infos){
        this.context = context;
        List<TimeInfo> list = new ArrayList<>();
        list.addAll(infos);
        this.infos = list;
    }

    public void getData(List<TimeInfo> list){
        infos.addAll(list);
        notifyDataSetChanged();
    }

    @NonNull
    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(context).inflate(R.layout.home_timeinfo_kid,parent,false);
        InfoHolder infoHolder = new InfoHolder(view);
        return infoHolder;
    }

    @Override
    public void onBindViewHolder(@NonNull RecyclerView.ViewHolder holder, int position) {
        InfoHolder infoHolder = (InfoHolder)holder;
        infoHolder.setData(position);
    }

    @Override
    public int getItemCount() {
        return infos.size();
    }

    class InfoHolder extends RecyclerView.ViewHolder {
        public InfoHolder(@NonNull View itemView) {
            super(itemView);
        }

        public void setData(int position){
            TimeInfo timeInfo = infos.get(position);
            ImageView head = itemView.findViewById(R.id.Tinfo_head);
            head.setImageResource(R.mipmap.user_head);
            TextView name = itemView.findViewById(R.id.Tinfo_name);
            name.setText(timeInfo.getName());
            TextView content = itemView.findViewById(R.id.Tinfo_content);
            content.setText(timeInfo.getContent());
            ImageView img1 = itemView.findViewById(R.id.Tinfo_img1);
            Glide.with(context).load("http://10.150.11.9:8086/chengcar/banners/"+timeInfo.getImg1()).into(img1);
            ImageView img2 = itemView.findViewById(R.id.Tinfo_img2);
            Glide.with(context).load("http://10.150.11.9:8086/chengcar/banners/"+timeInfo.getImg2()).into(img2);
            ImageView img3 = itemView.findViewById(R.id.Tinfo_img3);
            Glide.with(context).load("http://10.150.11.9:8086/chengcar/banners/"+timeInfo.getImg3()).into(img3);

            itemView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    Intent intent = new Intent(context, TimeInfoActivity.class);
                    intent.putExtra("img1",timeInfo.getImg1());
                    context.startActivity(intent);
                }
            });
        }

    }

}
